package com.lc.hongtai.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.adapter.Fourth_findAdapter2;
import com.lc.hongtai.adapter.NewJiaXiAdapter;
import com.lc.hongtai.conn.JsonInterest;
import com.lc.hongtai.view.FullyLinearLayoutManager;
import com.lc.hongtai.view.SimpleDividerItemDecoration;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.bound.BoundViewHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;


/**
 * Created by Administrator on 2016/4/17.
 */
public class JiaXiFragment extends AppV4Fragment {


    String mTitle;
    private RecyclerView.Adapter mAdapter;
    private FullyLinearLayoutManager mLayoutManager;

    private RecyclerView rv01;

    private PtrFrameLayout store_house_ptr_frame;
    private PtrClassicDefaultHeader header;

    public int nowPage = 1;
    public int totalPages = 1;
    public int pageSize = 0;
    public int totalRows = 0;

    private boolean canLoadMore = true;

    List<String> mDatas;
    List<String> mDatas2;


    public List<JsonInterest.Info.ListContent> list = new ArrayList<>();

    Fourth_findAdapter2 mAdapter2;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.fr_simple_card, null);
        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_jiaxi, null)));
        ButterKnife.bind(getActivity());
        mDatas = new ArrayList<String>();
        mDatas2 = new ArrayList<>();
        rv01 = (RecyclerView) view.findViewById(R.id.rv_01);
        rv01.setLayoutManager(new LinearLayoutManager(getActivity()));

        //rv01.setAdapter(mAdapter = new Fourth_findAdapter(getActivity(),mDatas));

        for (int i = 'A'; i < 'z'; i++) {
            mDatas.add("" + (char) i);
        }

       /* TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        card_title_tv.setText(mTitle);*/

        for (int i = 0; i < 9; i++) {
            mDatas2.add(i + "");
        }
        mAdapter2 = new Fourth_findAdapter2(getActivity(), mDatas2);

        mAdapter = new NewJiaXiAdapter(getActivity(), mDatas);

        rv01.setAdapter(mAdapter);
        // rv01.setAdapter(mAdapter = new JIaxiAdapter(getActivity(), mDatas));

        initView(view);
        initRefreshListener();
        //  getAccessToken(true,nowPage);
        return view;
    }


    private void initView(View view) {
        this.rv01 = (RecyclerView) view.findViewById(R.id.rv_01);
        //    this.ivback = (ImageView)  view.findViewById(R.id.iv_back);
        this.store_house_ptr_frame = (PtrFrameLayout) view.findViewById(R.id.store_house_ptr_frame);
        mLayoutManager = new FullyLinearLayoutManager(getActivity());
        header = new PtrClassicDefaultHeader(getActivity());
        store_house_ptr_frame.setHeaderView(header);
        rv01.setLayoutManager(mLayoutManager);
        // specify an adapter (see also next example)
        Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider3);
        rv01.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 40));
       /* mAdapter = new IncreasedInterestAdapter(getActivity(), list);
        rv01.setAdapter(mAdapter);*/
    }

    /*    private void initRefreshListener() {
            rv01.setOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    int lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
                    if (dy > 0 && canLoadMore && lastVisibleItemPosition + 1 == mAdapter.getItemCount()) {
                        canLoadMore = false;
                        if (nowPage < totalPages) {
                            getAccessToken(true,nowPage+1);
                        } else {
                            UtilToast.show(getActivity(), "没有更多了");
                        }
                    }
                }
            });

            store_house_ptr_frame.setPtrHandler(new PtrHandler() {
                @Override
                public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                    int firstVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findFirstCompletelyVisibleItemPosition();
                    return firstVisibleItemPosition == 0 || list.size() == 0;
                }

                @Override
                public void onRefreshBegin(PtrFrameLayout frame) {
                    list.clear();
                    nowPage = 1;
                    getAccessToken(false,nowPage);
                }
            });
            store_house_ptr_frame.addPtrUIHandler(new PtrUIHandler() {
                @Override
                public void onUIReset(PtrFrameLayout frame) {
                    header.onUIReset(frame);
                }

                @Override
                public void onUIRefreshPrepare(PtrFrameLayout frame) {
                    header.onUIRefreshPrepare(frame);
                }

                @Override
                public void onUIRefreshBegin(PtrFrameLayout frame) {
                    header.onUIRefreshBegin(frame);
                }

                @Override
                public void onUIRefreshComplete(PtrFrameLayout frame) {
                    header.onUIRefreshComplete(frame);
                }

                @Override
                public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
                    header.onUIPositionChange(frame, isUnderTouch, status, ptrIndicator);
                }
            });
        }
        private void getAccessToken(final boolean isShow,final int page) {
            new JsonAccessToken("user/interest", new AsyCallBack<JsonAccessToken.Info>() {
                @Override
                public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                    if (info != null) {
                        if (!TextUtils.isEmpty(info.access_token)) {
                            initData(info.access_token, isShow,page);
                        } else {
                            UtilToast.show(getActivity(), "安全验证失败！");
                            if (isShow) {
                                store_house_ptr_frame.refreshComplete();
                            }
                            canLoadMore = true;
                        }
                    }
                }

                @Override
                public void onFail(String toast, int type) throws Exception {
                    super.onFail(toast, type);
                    UtilToast.show(getActivity(), "安全验证失败！");
                    if (isShow) {
                        store_house_ptr_frame.refreshComplete();
                    }
                    canLoadMore = true;
                }
            }).execute(getActivity(), isShow);

        }



        private void initData(String access_token,final boolean isShow,final int page) {
            new JsonInterest(access_token,MyApplication.myPreferences.readUid(), String.valueOf(page), new AsyCallBack<JsonInterest.Info>() {

                @Override
                public void onStart(int type) throws Exception {
                    super.onStart(type);
                }

                @Override
                public void onSuccess(String toast, int type, JsonInterest.Info info) throws Exception {
                    if (info != null) {
                        list.addAll(info.list);
                        nowPage = info.page.nowPage;
                        totalPages = info.page.totalPages;
                        pageSize = info.page.pageSize;
                        totalRows = info.page.totalRows;
                        if (list.isEmpty()) {
                            UtilToast.show(getActivity(), "无相关记录！");
                        }
                    }
                }

                @Override
                public void onFail(String toast, int type) throws Exception {
                    super.onFail(toast, type);
                    UtilToast.show(getActivity(), JsonBonus.TOAST);
                }

                @Override
                public void onEnd(String toast, int type) throws Exception {
                    if (isShow) {
    //                    store_house_ptr_frame.refreshComplete();

                        store_house_ptr_frame.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                store_house_ptr_frame.refreshComplete();
                            }
                        }, 2000);
                    }
                    canLoadMore = true;
                    mAdapter.notifyDataSetChanged();
                }

            }).execute(getActivity(), isShow);
        }*/
    private void initRefreshListener() {
        rv01.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
                if (dy > 0 && canLoadMore && lastVisibleItemPosition + 1 == mAdapter.getItemCount()) {
                    canLoadMore = false;
                    if (nowPage < totalPages) {
                        //getAccessToken(true, nowPage + 1);
                    } else {
                        //UtilToast.show(InvestManageDetailActivity.this, "当前页已是最后一页");
                    }
                }
            }
        });

        store_house_ptr_frame.setPtrHandler(new PtrHandler() {


            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                int firstVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findFirstCompletelyVisibleItemPosition();
                return firstVisibleItemPosition == 0 || mDatas.size() == 0;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //   list.clear();
              /*  nowPage = 1;
                getAccessToken(false, nowPage);*/
            }
        });
        store_house_ptr_frame.addPtrUIHandler(new PtrUIHandler() {
            @Override
            public void onUIReset(PtrFrameLayout frame) {
                header.onUIReset(frame);
            }

            @Override
            public void onUIRefreshPrepare(PtrFrameLayout frame) {
                header.onUIRefreshPrepare(frame);
            }

            @Override
            public void onUIRefreshBegin(PtrFrameLayout frame) {
                header.onUIRefreshBegin(frame);
            }

            @Override
            public void onUIRefreshComplete(PtrFrameLayout frame) {
                header.onUIRefreshComplete(frame);
            }

            @Override
            public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
                header.onUIPositionChange(frame, isUnderTouch, status, ptrIndicator);
            }
        });
    }

}
